package net.sharpf2.api;

import java.awt.Graphics;
import net.sharpf2.api.util.Camera;
import net.sharpf2.sfcore.bot.model.ScriptModel;

/**
 *
 * @author Rick van Biljouw
 */
public abstract class Script extends Methods implements ScriptModel {

    private boolean initialized = false;
    private volatile boolean paused = false;

    @Override
    public void onStart() {
    }

    @Override
    public void onStop() {
    }

    @Override
    public void onPause() {
    }

    @Override
    public void onResume() {
    }

    @Override
    public void onBreak() {
    }

    @Override
    public void paint(Graphics g) {
    }

    public void mouseClicked(int x, int y) {
    }

    public void onMessageReceived(String message) {
    }

    @Override
    public void setPaused(boolean bln) {
        this.paused = bln;

        if (!this.paused) {
            synchronized (this) {
                notifyAll();
            }
        }
    }

    @Override
    public void run() {
        System.setErr(net.sharpf2.sfcore.Globals.err);
        System.setOut(net.sharpf2.sfcore.Globals.out);

        while (!Thread.currentThread().isInterrupted()) {
            if (Globals.CLIENT.getGamestate() > 2) {
                getController().getRandomHandler().poll();
                if (Globals.CLIENT.getGamestate() == 11 && Globals.CLIENT.getSettings().getData()[1437] > 0) {
                    if (!initialized) {
                        java.awt.EventQueue.invokeLater(new Runnable() {

                            public void run() {
                                Methods.sleep(2000);
                                onStart();
                                Camera.setPitch(true);
                            }
                            
                        });
                        initialized = true;
                    } else {
                        Globals.CONTROLLER.getScriptHandler().setCycleTime(loop());
                        sleep(Globals.CONTROLLER.getScriptHandler().getCycleTime());
                    }
                }

                while (paused && !Thread.currentThread().isInterrupted()) {
                    try {
                        synchronized (this) {
                            wait();
                        }
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
    }
}
